<?php
class Setting_model extends CI_Model {

	function __construct()
    {
        parent::__construct();
    }
	/**
	 * 新增一张点卡，code随机
	 */
	public function add($key, $value) {   
		$this->db->where('setting_name', $key);
		$this->db->from('settings');
		$count = $this->db->count_all_results();
		if ($count > 0) {
			$sql = "update settings  set setting_value = " . $this->db->escape($value) . "where setting_name=" . $this->db->escape($key) . " limit 1";
		} else {
			$sql = "insert into  settings  set setting_value = " . $this->db->escape($value) . ",setting_name=" . $this->db->escape($key) ;
		}
		return $this->db->query($sql);
	}
	
	public function find_all() {
		$result = $this->db->get('settings')->result_array();
		$return_array = array();
		foreach ($result as $r) {
			$return_array[$r['setting_name']] = $r['setting_value'];
		}
		return $return_array;
	}
	
	public function find_by_key($key) {
		$this->db->where('setting_name', $key);
		return $this->db->get('settings')->row_array();
	}
}